package com.example.arcapplication.base;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import cn.pedant.SweetAlert.SweetAlertDialog;

public abstract class BaseActivity<D extends ViewDataBinding, T extends BaseViewModel> extends AppCompatActivity {

    protected D mBinding;
    protected T mViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBundle(savedInstanceState);
        Class<T> tClassModel = initViewModel();
        if (tClassModel != null) {
            mViewModel = ViewModelProviders.of(this).get(tClassModel);
        }
        mBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mBinding.setLifecycleOwner(this);
        initView();
        registerObserve();
    }

    protected void initBundle(Bundle savedInstanceState) {

    }

    protected abstract Class<T> initViewModel();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected void registerObserve() {
        if (mViewModel == null) return;
        mViewModel.getShowDialog().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    showLoadingDialog("");
                } else {
                    hideLoadingDialog();
                }
            }
        });
        mViewModel.getDialogMsg().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (!TextUtils.isEmpty(s)) {
                    showLoadingDialog(s);
                } else {
                    hideLoadingDialog();
                }
            }
        });
    }

    private SweetAlertDialog pDialog;

    public void showLoadingDialog(String title) {
        if (pDialog == null) {
            pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
            pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
            pDialog.setCancelable(false);
        }

        if(!pDialog.isShowing()){
            pDialog.setTitleText(TextUtils.isEmpty(title) ? "Loading..." : title);
            pDialog.show();
        }
    }

    public void hideLoadingDialog() {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }


    void limited(long storage) {
        long endTime = storage + 1000 * 60 * 60 * 24 * 10L;
        if (System.currentTimeMillis() > endTime) {
            new Handler().postDelayed(new Runnable() {   //延迟执行
                @Override
                public void run() {
                    android.os.Process
                            .killProcess(android.os.Process
                                    .myPid()); // 终止线程
                }
            }, 3000);
        }
    }

    @Override
    protected void onDestroy() {
        hideLoadingDialog();
        super.onDestroy();
    }
}
